﻿using MyMeeting.Service;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MyMeeting.Common
{
    public class VideoViual : StackPanel
    {
        public VideoViual VCenter;
        public WrapPanel PnlContianer;
        public bool IsOpenVideo = false;
        public bool IsOpenAudio = false;
        public bool IsJoined = false;
        private uint uid;
        public uint Uid { get=>uid; set {

                if (value != uid)
                {
                    uid = value;
                    if (true)
                    {
                        Dispatcher.Invoke((Action)(() => { userText.Text = "用户" + uid; }));
                    }
                    else
                    {
                        userText.Text = "用户" + uid;
                    }
                }

               
                
            } }
        public string Token { get; set; }
        public string ChannelId { get; set; }

        public string UserName { get; set; }

        private D3DImageSource render_;

        public D3DImageSource Render { get => render_; private set { } }

        public uint Volume { get; internal set; }

        public Action<VideoViual> ChangeCenterEvent;
        public System.Windows.Controls.Image image = null;
        public TextBlock userText  = new TextBlock();

        public VideoViual(WrapPanel p, VideoViual center,string token,string channelId,uint uid)
        {
            PnlContianer = p;
           this.Background = System.Windows.Media.Brushes.Red;
            VCenter = center;
            
            this.Uid = uid;
            this.Token = token;
            ChannelId = channelId;
        }
      
        public void SetImage()
        {
            
             render_ = new D3DImageSource((int)this.Width, (int)this.Height, FrameFormat.ARGB32);

            // 将 D3DImage 绑定到 Image 控件
            var image = new System.Windows.Controls.Image
            {
                Stretch = Stretch.Fill

            };
            image.SetValue(Panel.ZIndexProperty, 1);
            image.Source = render_;
            image.Width = this.Width;
            image.Height = this.Height;

            this.Children.Add(image);

           
            userText.SetValue(Panel.ZIndexProperty, 20);
            userText.Text = "用户" + Uid;
            userText.TextAlignment = TextAlignment.Center;
            userText.Background = System.Windows.Media.Brushes.LightBlue;
            userText.Opacity = 0.7;
            userText.Margin = new Thickness(0, -20, 0, 0);
            userText.Width = 100;
            this.Children.Add(userText);
            //// this.AddVisualChild(image);
            //var btn = new Button();
            //btn.Margin = new Thickness(0, -200, 0, 0);
            //btn.SetValue(Panel.ZIndexProperty, 20);
            //btn.Background = System.Windows.Media.Brushes.Yellow;
            //btn.Content = "test";
            //btn.Width = 100;
            //btn.Height = 100;
            //this.Children.Add(btn);
        }
        public void Add()
        {
            PnlContianer.Children.Add(this);
        }
        private object obj_ = new object();

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if(VCenter!=null&&VCenter.Uid!=this.Uid)
            {
                VCenter.Uid=this.Uid;
                VCenter.IsOpenAudio = this.IsOpenAudio;
                VCenter.IsOpenVideo = this.IsOpenVideo;
                if(this.IsOpenVideo==false)
                {
                    VCenter.ClearBackground();
                }
                VCenter?. ChangeCenterEvent?.Invoke(this);
            }
        }

        public void LiveChannel()
        {
            if(true)
            {
                this.Dispatcher.Invoke(new Action(() => {
                    PnlContianer.Children.Remove(this);
                }));
            }
            else
            {
                PnlContianer.Children.Remove(this);
            }
        }

        public void DisableVideo()
        {
            this.ClearBackground();
        }
       
        public void ClearBackground()
        {
            render_?.Clear();  
        }

        public void DestroyRender()
        {
            if(Dispatcher.CheckAccess())
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    if (render_ != null)
                        render_.Dispose();
                    render_ = null;
                }));
            }
            else
            {
                if (render_ != null)
                    render_.Dispose();
                render_ = null;
            }
        }


        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            lock (obj_)
            {
                if (render_ != null)
                {
                    if (this.Dispatcher.CheckAccess())
                    {
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            render_.UpdateSize(sizeInfo.NewSize);
                        }));
                    }
                    else
                    {
                        render_.UpdateSize(sizeInfo.NewSize);
                    }
                }
            }
        }
        public void DeliverFrame(Agora.Rtc.VideoFrame videoFrame)
        {
            lock (obj_)
            {
                if (Dispatcher.CheckAccess())
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        render_.Present(videoFrame);
                    }));
                }
                else
                {
                    render_.Present(videoFrame);
                }
            }
        }
    }
}
